package com.cloudbench.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {

	/**
	 * Parse a string into a JSON Object.
     * 
	 * @param original
	 * @return
	 * @throws JSONException
	 */
	public static JSONObject jsonObject(String original) throws JSONException {
		return new JSONObject(original);
	}
	
	// Internal method to handle single sign-on backend for authorize().
	//     * Helper to validate an activity intent by resolving and checking the
    // provider's package signature.
	// Check if the access token requires refreshing.
	
	/**
	 * Parse a string into a JSON Object.
	 * 
	 * @param original
	 * @param key
	 * @return
	 * @throws JSONException
	 */
	public static JSONObject jsonObject(String original, String key) throws JSONException {
		return jsonObject(original).getJSONObject(key);
	}
	
	/**
	 * Get string value from original string data. 
	 * 
	 * @param original
	 * @param key
	 * @return
	 * @throws JSONException
	 */
	public static String getString(String original, String key) throws JSONException {
		return jsonObject(original).getString(key);
	}
	
	/**
	 * Get int value from original string data.
	 * 
	 * @param original
	 * @param key
	 * @return
	 * @throws JSONException
	 */
	public static int getInt(String original, String key) throws JSONException {
		return jsonObject(original).getInt(key);
	}
	
	/**
	 * Parse a string into a JSONArray Object in key of the original string. 
	 * 
	 * @param original
	 * @param key
	 * @return
	 * @throws JSONException
	 */
	public static JSONArray jsonArray(String original, String key) throws JSONException {
		return jsonObject(original).getJSONArray(key);
	}
	

}